#include "./test/cpp-httplib-v0.7.15/httplib.h"
#include<string>
#include"searcher.hpp"

using std::string;

const string web_root_path="./www_root";
const string goal_path="./data/goal_html";//去标签后的路径

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(goal_path);

    httplib::Server svr;
    svr.set_base_dir(web_root_path.c_str());

    //搜索的所有内容都在/s路径下
    svr.Get("/s",[&search](const httplib::Request& req,httplib::Response& rsp)\
    {
        if(req.has_param("word")==0)//word就是搜索关键字
        {
            rsp.set_content("必须要有搜索关键字！","text/plain:charset=utf-8");//浏览器以文本的形式返回给用户
            return;
        }
        //提取搜索关键字
        string key_word=req.get_param_value("word");
        cout<<"用户在搜索："<<key_word<<endl;

        string json_result;
        search.Search(key_word,json_result);

        //将json内容返回给用户
        rsp.set_content(json_result,"application/json");

        //rsp.set_content("hello world","text/plain:charset=utf-8");
    });

    svr.listen("0.0.0.0",8888);

    return 0;
}